# -*- coding:utf-8 -*-
"""
@Project :   PIEEngine
@File    :   esTool.py
@Time    :   2022/7/13 09:23
@Author  :   lishiwei
@Version :   1.0
@Contact :   shi_weihappy@126.com
@License :   (C)Copyright 2021-2022, lishiwei
@Desc    :   None清理FY3D L1在ES中的元数据
"""
import requests
import json
import datetime
from urllib.parse import unquote, quote
import logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

def encodeURIComponent(content, encoding="utf-8"):
    """
    编码
    :param content:
    :param encoding:
    :return:
    """
    new_content = str(content).encode(encoding)
    return quote(new_content, safe="")

def decodeURIComponent(content, encoding="utf-8"):
    """
    解码
    :param content:
    :param encoding:
    :return:
    """
    return unquote(content, encoding=encoding)

def queryAllIndex(ip, auth=None):
    """
    查询全部索引
    :param ip:
    :param auth:
    :return:
    """
    url = f"{ip}/_cat/indices?"
    logger.info(url)
    payload = ""
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.text

def queryData(ip, index, size=-1, condition=None, auth=None):
    """
    查询指定的数据
    :param ip:
    :param index:
    :param size:
    :param condition:
    :param auth:
    :return:
    """
    if size > 0:
        url = f"{ip}/{index}/_search?size={size}"
    else:
        url = f"{ip}/{index}/_search"
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    if condition:
        payload = json.dumps({
            "query": {
                "match": condition
            }
        })
    else:
        payload = json.dumps({
            "query": {
                "match_all": {}
            }
        })
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()

def queryDataByPage(ip, index, page=0, size=10, condition=None, auth=None):
    """
    分页查询指定的数据
    :param ip:
    :param index:
    :param page:
    :param size:
    :param condition:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_search"
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    if condition:
        payload = json.dumps({
            "query": {
                "match": condition
            },
            "from": page,
            "size": size
        })
    else:
        payload = json.dumps({
            "query": {
                "match_all": {}
            },
            "from": page,
            "size": size
        })
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()

def queryDataByCondition(ip, index, size=-1, condition=None, auth=None):
    """
    查询指定的数据
    :param ip:
    :param index:
    :param size:
    :param condition:
    :param auth:
    :return:
    """
    if size > 0:
        url = f"{ip}/{index}/_search?size={size}"
    else:
        url = f"{ip}/{index}/_search"
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    if condition:
        payload = json.dumps({
            "query": condition
        })
    else:
        payload = json.dumps({
            "query": {
                "match_all": {}
            }
        })
    response = requests.request("GET", url, headers=headers, data=payload)
    return response.json()


def createData(ip, index, id, data, auth=None):
    """
    增加数据
    :param ip:
    :param index:
    :param id:
    :param data:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_doc/{id}"

    payload = json.dumps(data)
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("POST", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()


def updateData(ip, index, id, data, auth=None):
    """
    更新数据
    :param ip:
    :param index:
    :param id:
    :param data:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_update/{id}"
    payload = json.dumps({
        "doc": data
    })
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("POST", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()


def deleteData(ip, index, id, auth=None):
    """
    删除数据
    :param ip:
    :param index:
    :param id:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_doc/{id}"
    payload = {}
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("DELETE", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()


def deleteIndex(ip, index, auth=None):
    """
    删除索引
    :param ip:
    :param index:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}"
    payload = {}
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("DELETE", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()


def deleteAllData(ip, index, auth=None):
    """
    删除指定索引的所有数据
    :param ip:
    :param index:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_doc/_delete_by_query"
    payload = json.dumps({
        "query": {
            "match_all": {}
        }
    })
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("POST", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()


def deleteDataByQuery(ip, index, condition, auth=None):
    """
    删除指定索引中指定条件的数据
    :param ip:
    :param index:
    :param condition:
    :param auth:
    :return:
    """
    url = f"{ip}/{index}/_doc/_delete_by_query"

    payload = json.dumps({
        "query": {
            "term": condition
        }
    })
    headers = {
        'Content-Type': 'application/json'
    }
    if auth:
        headers["Authorization"] = f'Basic {auth}'
    response = requests.request("POST", url, headers=headers, data=payload)
    logger.info(response.text)
    return response.json()

############################################################################
def queryAllItemFromIndex(name):
    """
    查询指定索引单个数据
    :param name:
    :return:
    """
    _index = f"engine.{name}.image"
    _ip = "http://10.25.21.3:30013"
    _auth = "ZWxhc3RpYzpodGh0QDIwMjI="
    result = queryData(_ip, _index, 10000, None, _auth)
    _hits = result.get("hits").get("hits")
    logger.info(f"total count is: {len(_hits)}")
    with open(f"./{_index}.json", "w+") as f:
        f.write(json.dumps({"hits": _hits}, indent=4))
    logger.info(f"============{name}============")
    logger.info(_hits[0])

def updateImagesId(name):
    _index = f"engine.{name}.image"
    # 线上ES
    _ip = "http://10.25.21.3:30013"
    _auth = "ZWxhc3RpYzpodGh0QDIwMjI="
    logger.info(f"process {_index}")
    _allData = queryData(_ip, _index, 100000, None, _auth)
    _hits = _allData.get("hits").get("hits")
    for item in _hits:
        _id = item.get("_id")
        logger.info(f"update {_index} id : {_id}")
        _ids = _id.split("/")
        # NSMC/FY3D/MERSI/L1/1000M/D/FY3D_MERSI_WHOLE_GLL_L1_20230514_0345_1000M/FY3D_MERSI_WHOLE_GLL_L1_20230514_0345_1000M
        if _ids[-2] == _ids[-1]:
            continue
        _scene_name = _ids[-1]
        _new_id = f"{_id}/{_scene_name}"
        _source = item.get("_source")
        _bucket = "bucket:pie-engine-default-tiles"
        metaData = {
            "name": _new_id,
            "time": _source.get("time"),
            "size": _source.get("size"),
            "uri": _bucket,
            "bands": _source.get("bands"),
            "centre": _source.get("centre"),
            "geom": _source.get("geom"),
            "properties": _source.get("properties")
        }
        # updateData(_ip, _index, encodeURIComponent(_id), _data, _auth)
        payload = json.dumps(metaData)
        _name = metaData["name"].replace("/", "%2F")
        es_server_url = f"http://10.25.21.3:30013/{_index}/_doc/{_name}"
        es_auth = "ZWxhc3RpYzpodGh0QDIwMjI="
        headers = {'Content-Type': 'application/json',
                   'Authorization': 'Basic {0}'.format(es_auth)}
        response = requests.request("POST", es_server_url, headers=headers, data=payload)
        result = json.loads(response.text)
        logger.info(result)

def deleteImages(name):
    _index = f"engine.{name}.image"
    # 线上ES
    _ip = "http://10.25.21.3:30013"
    _auth = "ZWxhc3RpYzpodGh0QDIwMjI="
    logger.info(f"process {_index}")
    _allData = queryData(_ip, _index, 100000, None, _auth)
    _hits = _allData.get("hits").get("hits")
    logger.info(f"total count is: {len(_hits)}")
    del_ids = []
    for item in _hits:
        _id = item.get("_id")
        _ids = _id.split("/")
        # if _ids[-2] == _ids[-1]:
        #     continue
        del_ids.append(_id)
    total = len(del_ids)
    index = 0
    for _id in del_ids:
        index += 1
        logger.info(f"delete {_id} step: {index / total * 100.0} %")
        deleteDataByQuery(_ip, _index, {
            "_id": _id
        }, _auth)
    logger.info(f"total is: {total}")

if __name__ == "__main__":
    logger.info("running ...")
    # queryAllItemFromIndex("fy3d_mersi_1000m_d")
    # updateImagesId("fy3d_mersi_1000m_d")
    deleteImages("fy3d_mersi_1000m_d")